import { type IpcRendererEvent, contextBridge } from "electron";
import { IpcRender } from "./helper/ipcRender";
import { Ipc_ChannelEnum } from "@enum/channel.enum";

contextBridge.exposeInMainWorld("ipc", {
  on: <T extends FreeArray>(key: Ipc_ChannelEnum, fn: IpcRenderListener<T>) =>
    IpcRender.on<T>(key, fn),
  once: <T extends FreeArray>(key: Ipc_ChannelEnum, fn: IpcRenderListener<T>) =>
    IpcRender.once<T>(key, fn),
  invoke: async <T extends FreeArray, V>(key: Ipc_ChannelEnum, ...args: T) =>
    await IpcRender.invoke<T, V>(key, ...args),
  send: <T extends FreeArray>(key: Ipc_ChannelEnum, ...args: T) =>
    IpcRender.send<T>(key, ...args),
  sendSync: <T extends FreeArray, V>(key: Ipc_ChannelEnum, ...args: T) =>
    IpcRender.sendSync<T, V>(key, ...args),
  removeListener: <T extends FreeArray>(
    key: Ipc_ChannelEnum,
    fn: IpcRenderListener<T>
  ) => IpcRender.removeListener<T>(key, fn),
  removeAllListeners: (key: Ipc_ChannelEnum) =>
    IpcRender.removeAllListeners(key),
  postMessage: (
    key: Ipc_ChannelEnum,
    message: Record<string, any>,
    transfer: MessagePort[]
  ) => IpcRender.postMessage(key, message, transfer),
});
